﻿Public Class Spotify2Skype

    ' Spotify2Skype by PhaxeNor
    ' Thanks to Steffset @ http://spotifyomatic.codeplex.com/

    Dim CheckTemptime As Long
    Dim Spotify As New spotify()
    Dim Skype As New SKYPE4COMLib.Skype

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    Private Sub Spotify2Skype_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        checkRunningProcess("Both")
        CheckTemptime = 0
        CheckTimer.Enabled = True
        CurrentSongInfo.Text = "No Song Playing"
        NotifyIcon1.ContextMenuStrip = Me.ContextMenuStrip1
    End Sub

    Private Function checkRunningProcess(ByVal c As String)

        Dim StartSpotifyRunning As Boolean = True
        Dim StartSkypeRunning As Boolean = True

        If c = "Spotify" Or c = "Both" Then
            ' Spotify Check
            Dim SpotifyProcess() As Process = System.Diagnostics.Process.GetProcessesByName("Spotify")

            If SpotifyProcess.Count <> 0 Then
                StartSpotify.Text = "Spotify is running"
                StartSpotify.Enabled = False
                CurrentSongInfo.Text = SpotifyInfo()
            Else
                StartSpotify.Text = "Start Spotify"
                StartSpotify.ForeColor = Color.Red
                StartSpotify.Enabled = True
                CurrentSongInfo.Text = "No Song Playing"
                StartSpotifyRunning = False
            End If
        End If

        If c = "Skype" Or c = "Both" Then
            ' Skype Check
            Dim SkypeProcess() As Process = System.Diagnostics.Process.GetProcessesByName("Skype")

            If SkypeProcess.Count <> 0 Then
                StartSkype.Text = "Skype is running"
                StartSkype.Enabled = False
                SkypeMessage()
            Else
                StartSkype.Text = "Start Skype"
                StartSkype.ForeColor = Color.Red
                StartSkype.Enabled = True
                StartSkypeRunning = False
            End If
        End If

        If StartSpotifyRunning = False Or StartSkypeRunning = False Then
            CheckTimer.Enabled = False
        Else
            CheckTimer.Enabled = True
        End If

    End Function

    Private Function SkypeMessage(Optional ByVal reset As Boolean = False)
        If Skype.Client.IsRunning And reset = False Then

            Dim Result As String = Replace(SkypeMoodMessage.Text, "%playing%", Spotify.Nowplaying())
            If Result = "" Then
                Result = ""
            End If
            Skype.CurrentUserProfile.RichMoodText = Result
        ElseIf reset = True Then
            Skype.CurrentUserProfile.RichMoodText = ""
        End If
    End Function

    Private Function SpotifyInfo()
        Dim active As String = Spotify.Nowplaying()
        If active = "" Then Return "No Song Playing"
        Return active
    End Function


    Private Sub StartSpotify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartSpotify.Click

        Dim readValue As String
        readValue = My.Computer.Registry.GetValue _
        ("HKEY_CURRENT_USER\Software\Spotify", "", Nothing)

        System.Diagnostics.Process.Start(readValue + "\spotify.exe") ' Start the process
        StartSpotify.Text = "Starting Spotify"
        StartSpotify.Enabled = False
        checkRunningProcess("Spotify") ' Check that spotify is running
    End Sub

    Private Sub StartSkype_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartSkype.Click

        Skype.Client.Start(True) ' Start the process
        StartSkype.Text = "Starting Skype"
        StartSkype.Enabled = False
        Sleep(6000) ' Sleep for 6sec to make sure skype has logged in.
        checkRunningProcess("Skype") ' Check that skype is running
    End Sub


    ' CheckTimer to update mood messages
    Private Sub CheckTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckTimer.Tick
        CheckTemptime = CheckTemptime + 1
        If CheckTemptime >= 20 Then
            checkRunningProcess("Both")
            CheckTemptime = 0
        End If
    End Sub


    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If Skype.Client.IsRunning = True Then ' Make sure Skype is running so we don't end up with an error.
            SkypeMessage(True)
        End If
    End Sub 'Form1_Closing

    Private Sub Spotify2Skype_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            Me.Hide()
        End If
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        If Me.WindowState = FormWindowState.Normal Then
            Me.Hide()
        Else
            Me.Show()
            Me.WindowState = FormWindowState.Normal
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub
End Class
